home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP9_91.ARJ / SCALDATE.C < prev    next >
Text File  |  1989-07-11  |  1KB  |  52 lines

  1. /* scalar date routines    --    public domain by Ray Gardner
  2. ** These will work over the range 1/01/01 thru 14699/12/31
  3. */
  4.  
  5. int isleap (yr)
  6. unsigned yr;
  7. {
  8.    return yr % 400 == 0 || (yr % 4 == 0 && yr % 100 != 0);
  9. }
  10.  
  11. static unsigned months_to_days (month)
  12. unsigned month;
  13. {
  14.    return (month * 3057 - 3007) / 100;
  15. }
  16.  
  17. static long years_to_days (yr)
  18. unsigned yr;
  19. {
  20.    return yr * 365L + yr / 4 - yr / 100 + yr / 400;
  21. }
  22.  
  23. long ymd_to_scalar (yr, mo, day)
  24. unsigned yr, mo, day;
  25. {
  26.    long scalar;
  27.    scalar = day + months_to_days(mo);
  28.    if ( mo > 2 )                         /* adjust if past February */
  29.       scalar -= isleap(yr) ? 1 : 2;
  30.    yr--;
  31.    scalar += years_to_days(yr);
  32.    return scalar;
  33. }
  34.  
  35. void scalar_to_ymd (scalar, pyr, pmo, pday)
  36. long scalar;
  37. unsigned *pyr, *pmo, *pday;
  38. {
  39.    unsigned n;                /* compute inverse of years_to_days() */
  40.    for ( n = (scalar * 400L) / 146097; years_to_days(n) < scalar; )
  41.       n++;                          /* 146097 == years_to_days(400) */
  42.    *pyr = n;
  43.    n = scalar - years_to_days(n-1);
  44.    if ( n > 59 ) {                       /* adjust if past February */
  45.       n += 2;
  46.       if ( isleap(*pyr) )
  47.          n -= n > 62 ? 1 : 2;
  48.    }
  49.    *pmo = (n * 100 + 3007) / 3057;  /* inverse of months_to_days() */
  50.    *pday = n - months_to_days(*pmo);
  51. }
  52.